package com.heo.web.controller;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.heo.web.entity.KaoLaDeviceInfo;
import com.heo.web.util.GsonUtils;
import com.heo.web.util.RedisUtil;
import com.heo.web.util.Utils;



@Controller
public class InitDataController {

	private final Logger logger = LoggerFactory.getLogger(InitDataController.class);
	
    
	@Autowired
	private RedisUtil redisUtill;

    
	@RequestMapping("/redis/kaola")
	public String about(HttpServletRequest request,HttpServletResponse response, Model model) throws IOException {
		String path = this.getClass().getResource("/").getPath() + "kaola.txt";
		System.out.println(path);
		readFile(path);
		return "index";
	}
	
	/*@RequestMapping("/redis")
	public String redis(HttpServletRequest request,HttpServletResponse response, Model model) {
		KaoLaDeviceInfo kaoLaDeviceInfo = null;
		String jsonString =  (String) redisUtill.getValue("0ZHDS0000004");
		 kaoLaDeviceInfo = (KaoLaDeviceInfo) GsonUtils.jsonToObject(jsonString, KaoLaDeviceInfo.class);
		System.err.println(kaoLaDeviceInfo.toString());
		return "index";
	}*/
	
	
	/**
     * 读文件
     * @param fileName
     * @return
     */
    public void readFile(String fileName){
        FileReader input = null;
        BufferedReader reader = null;
        try{
            input = new FileReader(fileName);
            reader = new BufferedReader(input);
            String line = null;
            while((line = reader.readLine()) != null){
            	System.out.println(line);
                KaoLaDeviceInfo kaoLaDeviceInfo = null;
                kaoLaDeviceInfo = (KaoLaDeviceInfo) GsonUtils.jsonToObject(line, KaoLaDeviceInfo.class);
                String macAddress = kaoLaDeviceInfo.getMacAddress();
                if(Utils.checkNull(macAddress)){
                	redisUtill.setValue(macAddress, line);
                }
                
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(null != reader){
                try {
                    reader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(null != input){
                try {
                    input.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    
    
    
}
	
	
	
